import mx.utils.Delegate;

import org.molamil.acto.commands.CommandContext;
import org.molamil.acto.commands.CommandRequest;
import org.molamil.acto.commands.CommandResult;
import org.molamil.acto.commands.CommandsConf;
import org.molamil.acto.commands.cmd.AbstractRemoteCmd;
import org.molamil.acto.core.EventConstants;
import org.molamil.acto.core.Logger;

/**
 * @author Jorge Hernandez
 */
class org.molamil.acto.commands.cmd.XmlResponseCmd extends AbstractRemoteCmd {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("XmlResponseCmd");
	
	private var xml:XML;
	private var xmlLoadDelegate:Function;
	
	
	// Constructor
	
	public function XmlResponseCmd(context:CommandContext) {
		super(context);
		xmlLoadDelegate = Delegate.create(this, onXmlLoadListener);
	}
	
	
	// Functions
	
	public function execute(request:CommandRequest):CommandResult {
		
		_result = super.execute(request);
		
		if (!_result.isValidated) {
			_result.status = CommandResult.STATUS_ERROR;
			var eventObject:Object = {target: this, type: EventConstants.COMMAND_EXECUTE_ERROR, name: _request.name, 
									  result: _result.result};
			dispatchEvent(eventObject);
			return _result;
		}
		
		var url:String = _context.target;
		
		// Create the URL with the params.
		if (!CommandsConf.isOmmitParams) {
			
			var qs:String = getQueryString(request.params);
			
			if (qs != "")
				url += (url.indexOf("?") == -1 ? "?" : "&");
				
			url += qs;
			
		}
		
		log.debug("execute(), url = " + url);
		
		xml = new XML();
		xml.ignoreWhite = true;
		xml.onLoad = xmlLoadDelegate;
		xml.load(url);
		
		_result.status = CommandResult.STATUS_PENDING;
		
		return _result;
		
	}

	private function getQueryString(o:Object) {
		
		var s:String = "";
		for (var prop in o)
			s += prop + "=" + o[prop] + "&";
			
		// Add a foo var if cache == "clear"
		if (_context.cache.toLowerCase() == CommandContext.CACHE_CLEAR.toLowerCase())
			s += "foo=" + (new Date()).getTime(); 
			
		// Remove the last &
		s = s.substr(0, s.length - 1);
		
		return s;
		
	}
	
	private function onXmlLoadListener(success:Boolean):Void {
		log.debug("onXmlLoadListener: " + xml);
		if (success) {
			var type:String = EventConstants.COMMAND_EXECUTE_COMPLETE;
			try {
				_result.result = onStatus(xml);
				_result.status = CommandResult.STATUS_OK;
			} catch (e:org.molamil.acto.commands.CommandResultError) {
				_result.result = e.result;
				_result.status = CommandResult.STATUS_ERROR;
				type = EventConstants.COMMAND_EXECUTE_ERROR;
			}
			var eventObject:Object = {target: this, type: type, name: _request.name, result: _result.result};
			dispatchEvent(eventObject);
		} else {
			_result.result = onFault(xml);
			_result.status = CommandResult.STATUS_ERROR;
			var eventObject:Object = {target: this, type: EventConstants.COMMAND_EXECUTE_ERROR, name: _request.name, 
			 						  result: _result.result};
			dispatchEvent(eventObject);	
		}
	}
	
	private function onStatus(response:Object):Object {
		return response;
	}
	
	private function onFault(response:Object):Object {
		return response;
	}
	
	
}
